package com.wzgl.core.controller;

import java.math.BigInteger;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.wzgl.core.base.JBaseController;
import com.wzgl.core.interceptor.RequiredUserIdInterceptor;
import com.wzgl.core.model.Tdicinfo;
import com.wzgl.core.model.Tdicsort;
import com.wzgl.core.utils.StringUtils;
import com.wzgl.core.utils.UuidUtil;
import com.wzgl.core.validate.TdicInfoValidate;
import com.wzgl.core.validate.TdicSortValidate;

public class TdicinfoContraller extends JBaseController {
	/***
	 * 分类管理
	 * @throws Exception
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void dic_index() throws Exception {
		List<Tdicsort> sorts = Tdicsort.dao.getAll();
		setAttr("sorts", sorts);
		render("dic_list.jsp");
	} 
	/**
	 * 字典编辑(新增 修改)
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void tdic_edit(){
		String fid = getPara("fid");
		Tdicinfo t =null;
		List<Tdicsort> sorts = Tdicsort.dao.getAll();
		setAttr("sorts", sorts);
		if(StrKit.notBlank(fid)){
			t = Tdicinfo.dao.findById(fid);
			setAttr("tdicinfo", t);
		}
		render("tdicinfo_edit.jsp");
	}
	/***
	 * 获取作者管理列表
	 * @throws Exception
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void dic_list() throws Exception {
		Page<Tdicinfo> list_page;
		try {
			BigInteger page=getParaToBigInteger("page",BigInteger.valueOf(1));
			BigInteger pagesize=getParaToBigInteger("pagesize",BigInteger.valueOf(5));
			String fvalue =  getParaTo("fvalue");
			String fsort = getParaTo("fsortcode");
			list_page= Tdicinfo.dao.getListByVal(page.intValue(), pagesize.intValue(),fvalue,fsort);
			renderAjaxResult("",0,list_page);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			renderAjaxResultForError(e.getMessage());
		}
	}
	public void tdic_sort() throws Exception {
		render("tdicsort_list.jsp");
	}
	/**
	 * 字典分类
	 * @throws Exception
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void tdicsort_list() throws Exception {
		Page<Tdicsort> list_page;
		try {
			BigInteger page=getParaToBigInteger("page",BigInteger.valueOf(1));
			BigInteger pagesize=getParaToBigInteger("pagesize",BigInteger.valueOf(5));
			String fvalue =  getParaTo("fvalue");
			list_page= Tdicsort.dao.getListByVal(page.intValue(), pagesize.intValue(),fvalue,null);
			renderAjaxResult("",0,list_page);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			renderAjaxResultForError(e.getMessage());
		}
	}
	/**
	 * 字典分类编辑(新增 修改)
	 */
	@Before(RequiredUserIdInterceptor.class)
	public void tdicsort_edit(){
		String fid = getPara("fid");
		Tdicsort t =null;
		if(StrKit.notBlank(fid)){
			t = Tdicsort.dao.findById(fid);
			setAttr("tdicsort", t);
		}
		render("tdicsort_edit.jsp");
	}
	/**
	 *删除字典分类(设为失效)
	 */
	public void delTdic(){
		String fid = getPara("fid");
		Tdicsort t =null;
		Ret r = new Ret();
		boolean b = false;
		if(StrKit.notBlank(fid)){
			t = Tdicsort.dao.findById(fid);
			if(t != null){
				t.setFstats(-1);
				b = t.update();
			}
		}
		if(b){
			renderJson(r.ok());
		}else{
			renderJson(r.fail("msg", "删除失败,请稍后再试!"));
		}
	}
	public void delTdicinfo(){
		String fid = getPara("fid");
		Tdicinfo t =null;
		Ret r = new Ret();
		boolean b = false;
		if(StrKit.notBlank(fid)){
			t = Tdicinfo.dao.findById(fid);
			if(t != null){
				t.setFstats(-1);
				b = t.update();
			}
		}
		if(b){
			renderJson(r.ok());
		}else{
			renderJson(r.fail("msg", "删除失败,请稍后再试!"));
		}
	}
	/**
	 * 保存字典信息
	 */
	@Before(TdicSortValidate.class)
	public void tdicsortSave(){
		Tdicsort t = getModel(Tdicsort.class);
		Integer fstats = StringUtils.isOn(getPara("fstats"));
		t.setFstats(fstats);
		boolean b = false;
		Tdicsort s = Tdicsort.dao.findBySort(t.getFsortcode());
		if((t.getFid() == null && s!= null) || s != null && !s.getFid().equals(t.getFid())){
			renderJson(Ret.fail("msg", "分类编码已经存在,请更换!"));
			return;
		}
		if(StrKit.isBlank(t.getFid())){
			t.setFid(UuidUtil.get32UUID());
			b = t.save();
		}else{
			b = t.update();
		}
		if(b){
			renderJson(Ret.ok());
		}else{
			renderJson(Ret.fail("msg", "保存失败,请稍后再试!"));
		}
	}
	/***
	 * 保存字典分类
	 */
	@Before(TdicInfoValidate.class)
	public void tdicinfoSave(){
		Tdicinfo t = getModel(Tdicinfo.class);
		Integer fstats = StringUtils.isOn(getPara("fstats"));
		t.setFstats(fstats);
		Ret r = new Ret();
		boolean b = false;
		Tdicinfo s = Tdicinfo.dao.findByCode(t.getFcode());
		if(s != null && !s.getFid().equals(t.getFid())){
//		if((t.getFid() == null && s!= null) || s != null && !s.getFid().equals(t.getFid())){
			renderJson(r.fail("msg", "字典编码已经存在,请更换!"));
			return;
		}
		if(StrKit.isBlank(t.getFid())){
			t.setFid(UuidUtil.get32UUID());
			b = t.save();
		}else{
			b = t.update();
		}
		if(b){
			renderJson(r.ok());
		}else{
			renderJson(r.fail("msg", "保存失败,请稍后再试!"));
		}
	}
}
